package com.mooc.ppjoke.ui.detail;

import android.view.ViewGroup;

import androidx.annotation.CallSuper;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.paging.PagedList;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mooc.lib_common.EmptyView;
import com.mooc.lib_common.PixUtils;
import com.mooc.ppjoke.R;
import com.mooc.ppjoke.data.Comment;
import com.mooc.ppjoke.data.Feed;
import com.mooc.ppjoke.databinding.LayoutFeedDetailBottomInateractionBinding;

/**
 * @ClassName: ViewHandler$
 * @Description: java类作用描述
 * @Author: 魏靳元
 * @CreateDate: 2022/8/26 08:16
 * @UpdateRemark: 更新内容
 * @Version: 1.0
 */
public abstract class ViewHandler {
    protected FragmentActivity activity;
    protected Feed mFeed;
    protected RecyclerView rv;
    protected LayoutFeedDetailBottomInateractionBinding mBottomBinding;
    protected FeedCommentAdapter adapter;
    protected FeedDetailViewModel viewModel;
    public void onPause() {

    }

    public void onResume() {

    }

    public void onBackPressed() {

    }
    public ViewHandler(FragmentActivity activity){

        this.activity = activity;
        viewModel = ViewModelProviders.of(activity).get(FeedDetailViewModel.class);

    }
    @CallSuper//保证父类功能正常
    public void bindInitData(Feed feed) {
        mBottomBinding.setOwner(activity);
        mFeed=feed;
        rv.setLayoutManager(new LinearLayoutManager(activity,LinearLayoutManager.VERTICAL,false));
        rv.setItemAnimator(null);
        adapter = new FeedCommentAdapter();
        rv.setAdapter(adapter);
        viewModel.setItemId(mFeed.itemId);
        viewModel.getLiveData().observe(activity, new Observer<PagedList<Comment>>() {
            @Override
            public void onChanged(PagedList<Comment> comments) {
                adapter.submitList(comments);
                handleEmpty(comments.size() > 0);
            }
        });
    }
    private EmptyView emptyView;
    protected void handleEmpty(boolean hasData) {
        if (hasData) {
            if (emptyView != null) {
                adapter.removeHeaderView(emptyView);
            }
        } else {
            if (emptyView == null) {
                emptyView = new EmptyView(activity);
                RecyclerView.LayoutParams layoutParams = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                layoutParams.topMargin = PixUtils.dp2px(40);
                emptyView.setLayoutParams(layoutParams);
                emptyView.setEmptyText(activity.getString(R.string.feed_comment_empty));
            }
            adapter.addHeaderView(emptyView);
        }

    }
}
